package com.ccjq.shosestore.web.controller.member;

import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.member.service.UserService;
import com.ccjq.shosestore.member.service.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    private static final String FALG_REG ="reg";//注册页面
    private static final String FALG_REG_SUBMIT = "rs";//提交注册

    private static final String FALG_LOGIN = "login";//登录页面
    private static final String FALG_LOGIN_SUBMIT = "ls";//提交登录

    private static final String FALG_INFO = "info";//用户中心页面
    private static final String FALG_LOGOUT = "logout";//注销
    private static final String FALG_FAILED = "failed";//失败

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String flag = request.getParameter("flag");
        System.out.println("flag=" + flag);
        if(StringUtils.isBlank(flag)){
            //跳转到登录页面
            //toLoginPage(request,response);
        }else if(FALG_REG.equals(flag)){
            //跳转到注册页面
            toRegisterPage(request,response);
        }else if(FALG_REG_SUBMIT.equals(flag)){
            //执行注册
            register(request,response);
        }else if(FALG_INFO.equals(flag)){
            //跳转到用户界面
            toUserInfo(request,response);
        }else if(FALG_LOGOUT.equals(flag)){
            //用户注销页面
            logout(request,response);
        }else if(FALG_LOGIN.equals(flag)){
            //跳转到登录页面
            toLoginPage(request,response);
        }else if(FALG_LOGIN_SUBMIT.equals(flag)){
            //执行登录
            login(request,response);
        }else if(FALG_FAILED.equals(flag)){
            //登录失败
            failed(request,response);
        }

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request,response);
        this.doPost(request,response);
    }
    private void register(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        String username = request.getParameter("username");
        String mobilePhone = request.getParameter("phone");
        String password = request.getParameter("password");
        String vicode = request.getParameter("vicode");
        HttpSession session = request.getSession();
        String checkcode = (String) session.getAttribute("checkcode");
        System.out.println("checkcode=" + checkcode+"；vicode="+vicode);
        if (vicode.equalsIgnoreCase(checkcode)){
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setMobilePhone(mobilePhone);
            String userId = userService.saveUser(user);
            User user2 = userService.findUserByid(userId);
            session.setAttribute("user",user2);
            request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);
            return;
        }else {
            request.setAttribute("message","验证码不正确");
            request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request,response);
        }
    }
    protected void toRegisterPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request,response);

    }
    protected void toLoginPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);

    }
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String vicode = request.getParameter("vicode");

        HttpSession session = request.getSession();
        String checkcode = (String) session.getAttribute("checkcode");
        System.out.println("@checkcode=" + checkcode+"；@vicode="+vicode);
        if (vicode.equalsIgnoreCase(checkcode)){
            User user = userService.findUserByNamePw(username,password);
            if(user!=null){
                String userId= user.getUserId();
                session.setAttribute("user",user);
                session.setAttribute("userId",userId);
                request.getRequestDispatcher("/WEB-INF/jsp/home/homepageadmin.jsp").forward(request,response);
                return;
            }else{
                request.getRequestDispatcher("/WEB-INF/jsp/member/false.jsp").forward(request,response);
            }
        }else {
            request.setAttribute("message","验证码不正确");
            request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
        }
    }
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();
        request.getRequestDispatcher("/homepage.jsp").forward(request,response);
    }
    protected void toUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request,response);

    }
    protected void failed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);

    }
}
